package com.rwind.dynamic.common;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;



public class MessageDto<E> {
  public final static String CODE_SUCCESS = "0" ;

  public final static String CODE_FAILURE = "100";
  
  /**
   * 状态码 0:成功 非0全部失败
   */
  
  private String code;

  private String originCode;
  /**
   * 失败时的错误信息
   */
  
  private String msg;
  private E data;


  //private String seqNo;

  public boolean isOk() {
	  return this.getCode().equals(CODE_SUCCESS); 
  }

  public static <E> MessageDto<E> getSuccess(E data) {
    MessageDto<E> msg = new MessageDto<E>();
    msg.setCode(CODE_SUCCESS);
    msg.setMsg("操作成功");
    msg.setData(data);
    return msg;
  }

  public static <E> MessageDto<E> getSuccess(E data,String seqNo) {
    MessageDto<E> msg = new MessageDto<E>();
    msg.setCode(CODE_SUCCESS);
    msg.setMsg("操作成功");
    msg.setData(data);
    //msg.setSeqNo(seqNo);
    return msg;
  }


  public static <E> MessageDto<E> getFail(E data, String code, String message) {
    MessageDto<E> msg = new MessageDto<E>();
    msg.setCode(code);
    msg.setMsg(message);
    msg.setData(data);
    return msg;
  }

  public static <E> MessageDto<E> getFail(E data,  String message) {
    MessageDto<E> msg = new MessageDto<E>();
    msg.setCode("90000");
    msg.setMsg(message);
    msg.setData(data);
    return msg;
  }

  public static <E> MessageDto<E> getFail(String code, String message) {
    MessageDto<E> msg = new MessageDto<E>();
    msg.setCode(code);
    msg.setMsg(message);
    msg.setData(null);
    return msg;
  }

  public static <E> MessageDto<E> getFail(String message) {
    MessageDto<E> msg = new MessageDto<E>();
    msg.setCode("90000");
    msg.setMsg(message);
    msg.setData(null);
    return msg;
  }

  /**
   * 获取返回结果code
   *
   * @return code
   */
  public String getCode() {
    return code;
  }

  /**
   * 设置返回结果code
   *
   * @param code the code
   */
  public void setCode(String code) {
    this.code = code;
  }

  /**
   * 获取返回结果消息
   *
   * @return the msg
   */
  public String getMsg() {
    return msg;
  }

  /**
   * 设置返回结果消息
   *
   * @param msg the msg
   */
  public void setMsg(String msg) {
    this.msg = msg;
  }

  /**
   * 获取返回结果数据
   *
   * @return the data
   */
  public E getData() {
    return data;
  }

  /**
   * 设置返回结果数据
   *
   * @param data the data
   */
  public void setData(E data) {
    this.data = data;
  }

  public static <E> MessageDto<E> initSuccess(E body) {
    MessageDto<E> responseDto = new MessageDto<E>();
    responseDto.setCode(CODE_SUCCESS);
    responseDto.setMsg("操作成功");
    responseDto.setData(body);
    return responseDto;
  }



  public static Map<String, Object> initSuccess() {
    Map<String, Object> map = new HashMap<>();
    map.put("code", 0);
    map.put("msg", "操作成功");
    return map;
  }


  public static <E> MessageDto<E> initFail(E body, String message) {
    MessageDto<E> responseDto = new MessageDto<E>();
    responseDto.setCode("1");
    responseDto.setMsg(message);
    responseDto.setData(body);
    return responseDto;
  }

  public static <E> MessageDto<E> initFail(E body, String code, String message) {
    MessageDto<E> responseDto = new MessageDto<E>();
    responseDto.setCode(code);
    responseDto.setMsg(message);
    responseDto.setData(body);
    return responseDto;
  }


  @JsonIgnore
  public String getOriginCode() {
    return originCode;
  }

  public void setOriginCode(String originCode) {
    this.originCode = originCode;
  }




}
